package com.example.gzysmartcity.ui.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.ActivityMainBinding;
import com.example.gzysmartcity.ui.adapter.MainVp2Adapter;
import com.example.gzysmartcity.ui.fragment.idea.IdeaFragment;
import com.example.gzysmartcity.ui.fragment.home.HomeFragment;
import com.example.gzysmartcity.ui.fragment.me.MeFragment;
import com.example.gzysmartcity.utils.NetworkUtil;
import com.example.gzysmartcity.viewmodel.MainViewModel;
import com.permissionx.guolindev.PermissionX;

import java.util.Arrays;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding b;
    private MenuItem menuItem;


    @SuppressLint("NonConstantResourceId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        switch (SPUtils.getInstance("theme", Context.MODE_PRIVATE).getInt("theme", 1)) {
            case 1:
                setTheme(R.style.Theme_GZYSmartCity);
                break;
            case 2:
                setTheme(R.style.Theme_GZYSmartCity_Yellow);
                break;
            case 3:
                setTheme(R.style.Theme_GZYSmartCity_Green);
                break;
            case 4:
                setTheme(R.style.Theme_GZYSmartCity_Blue);
                break;
            case 5:
                setTheme(R.style.Theme_GZYSmartCity_Black);
                break;
            case 6:
//                if (this.getApplicationContext().getResources().getConfiguration().uiMode == 0x21) {
//                setTheme();
//                }
                break;
        }
        super.onCreate(savedInstanceState);
        b = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(b.getRoot());
        MainViewModel mainViewModel = new ViewModelProvider(this).get(MainViewModel.class);
        NetworkUtils.registerNetworkStatusChangedListener(new NetworkUtils.OnNetworkStatusChangedListener() {
            @Override
            public void onDisconnected() {
                ToastUtils.showShort("断开连接");
            }

            @Override
            public void onConnected(NetworkUtils.NetworkType networkType) {
                String s = "";
                switch (networkType) {
                    case NETWORK_ETHERNET:
                        break;
                    case NETWORK_WIFI:
                        s = "wifi";
                        break;
                    case NETWORK_5G:
                        s = "5G";
                        break;
                    case NETWORK_4G:
                        s = "4G";
                        break;
                    case NETWORK_3G:
                        s = "3G";
                        break;
//                    case NETWORK_2G:
//                        break;
//                    case NETWORK_UNKNOWN:
//                        break;
//                    case NETWORK_NO:
//                        break;
                }
                ToastUtils.showShort(s + "网络以连接");
            }
        });
        requestPermission();
        mainViewModel.getLiveData().observe(this, aBoolean -> {
            if (aBoolean) {
                b.bottomMain.setVisibility(View.VISIBLE);
            } else {
                b.bottomMain.setVisibility(View.GONE);
            }
        });
        //无登录，点击【退出登录】后重置当页包含的个人信息，并且点击底部导航栏“主页”和“创意设计”会Toast提示【您还未登录】
        if (NetworkUtil.userId == null) {
            Log.i("TAG", "无登录");
            b.bottomMain.setSelectedItemId(R.id.me);
            b.vp2Main.setEnabled(false);
            MainVp2Adapter mainVp2Adapter = new MainVp2Adapter(getSupportFragmentManager(), getLifecycle(),
                    Collections.singletonList(new MeFragment()));
            b.vp2Main.setAdapter(mainVp2Adapter);
            b.bottomMain.setOnNavigationItemSelectedListener(item -> {
                switch (item.getItemId()) {
                    case R.id.home:
                    case R.id.idea:
                        ToastUtils.showShort("您还未登录");
                        break;
                }
                return false;
            });
            return;
        }
        //有登录
        MainVp2Adapter mainVp2Adapter = new MainVp2Adapter(getSupportFragmentManager(), getLifecycle(),
                Arrays.asList(new HomeFragment(), new IdeaFragment(), new MeFragment()));
        b.vp2Main.setOffscreenPageLimit(2);
        b.vp2Main.setAdapter(mainVp2Adapter);
        Menu menu = b.bottomMain.getMenu();
        menuItem = menu.getItem(0);
        b.vp2Main.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                menuItem.setChecked(false);
                menuItem = menu.getItem(position);
                menuItem.setChecked(true);
            }
        });
        b.bottomMain.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.home:
                    b.vp2Main.setCurrentItem(0, false);
                    break;
                case R.id.idea:
                    b.vp2Main.setCurrentItem(1, false);
                    break;
                case R.id.me:
                    b.vp2Main.setCurrentItem(2, false);
                    break;
            }
            return true;
        });
    }


    //    <!-- 这个权限用于进行网络定位-->
    //    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    //    <!-- 这个权限用于访问GPS定位-->
    //    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    //    <!-- 用于访问wifi网络信息，wifi信息会用于进行网络定位-->
    //    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    //    <!-- 获取运营商信息，用于支持提供运营商信息相关的接口-->
    //    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    //    <!-- 这个权限用于获取wifi的获取权限，wifi信息会用来进行网络定位-->
    //    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    //    <!-- 访问网络，网络定位需要上网-->
    //    <uses-permission android:name="android.permission.INTERNET" />
    private void requestPermission() {
        PermissionX.init(this)
                .permissions(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE,
                        Manifest.permission.CAMERA, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_COARSE_LOCATION
                        , Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE
                )
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
//                        ToastUtils.showShort("全部权限授权成功");
                        return;
                    }
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < deniedList.size(); i++) {
                        stringBuilder.append(deniedList.get(i));
                        if (i != deniedList.size() - 1) {
                            stringBuilder.append("、");
                        }
                    }
                    ToastUtils.showShort(stringBuilder.toString() + "未授权。");
                });
    }
}